{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dmiY5io3vjmu"
   },
   "source": [
    "# Chapter 2 - Datatypes and Operators：資料型態與運算子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3hHJ1yHgSoCR"
   },
   "source": [
    "電腦發展快速所帶來的直接效益，就是計算量大大地增加了，而計算正是一個讓電腦幫我們做事情的入門點。本章節我們先從簡單的數學運算開始介紹 Python 的功能、以及一些不同的資料型態 (Datatype)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "X6UpX9khvvBJ"
   },
   "source": [
    "## Numbers：數字類的資料型態"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "n0r1JDXIU3_m"
   },
   "source": [
    "數字的資料型態有兩種，分別是：\n",
    "\n",
    "* `int`：整數 (Integer)\n",
    "* `float`：浮點數 (Float)\n",
    "\n",
    "References:\n",
    "\n",
    "* [Numbers - Python Documentation](https://docs.python.org/3/tutorial/introduction.html#numbers)\n",
    "* [int - Python Documentation](https://docs.python.org/3/library/functions.html#int)\n",
    "* [float - Python Documentation](https://docs.python.org/3/library/functions.html#float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 893,
     "status": "ok",
     "timestamp": 1607329666644,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "mwWzZc6VFxzA",
    "outputId": "5289e15d-45ba-41d0-cf5e-1124f7239784"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2.0\n"
     ]
    }
   ],
   "source": [
    "# 試著顯示數字在畫面上\n",
    "print(1)\n",
    "print(2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "0KjO9RJ3GISo"
   },
   "source": [
    "配合 `type()` 函式，我們可以觀察不同樣態的數字，分別是什麼資料型態。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1010,
     "status": "ok",
     "timestamp": 1606721974334,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "GTkRlVIwFzkv",
    "outputId": "1b12aa47-028c-4f15-df06-de69402d4995"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "# 顯示整數 (int) 的資料型態\n",
    "print(type(1))\n",
    "# 顯示浮點數 (float)的資料型態\n",
    "print(type(2.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7OuiLd1VFu0I"
   },
   "source": [
    "### Opeators：運算子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "pt2vA0CVTTdG"
   },
   "source": [
    "程式是由許多的表達式 (Expression) 組成的，最直覺的表達式就是使用各種運算子執行四則運算：加 `+`、減 `-`、乘 `*`、除 `/`。除此之外，還有一些特別的運算：\n",
    "\n",
    "References:\n",
    "\n",
    "* [Mapping Operators to Functions - Python Documentation](https://docs.python.org/3/library/operator.html#mapping-operators-to-functions)\n",
    "* [Operator precedence - Python Documentation](https://docs.python.org/3/reference/expressions.html#operator-precedence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1703,
     "status": "ok",
     "timestamp": 1607330012732,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "pQf-bsE2vTzl",
    "outputId": "239f9b6d-3a14-4b71-df02-033e9a9c838a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n",
      "30\n",
      "1.1428571428571428\n"
     ]
    }
   ],
   "source": [
    "print(1 + 2)  # Addition：加法運算\n",
    "print(4 - 3)  # Substraction：減法運算\n",
    "print(5 * 6)  # Multiplication：乘法運算\n",
    "print(8 / 7)  # Division：除法運算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1059,
     "status": "ok",
     "timestamp": 1606716571900,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "kWheV8mhwmIX",
    "outputId": "e64c8196-025a-4b9f-8ffe-2b74f58f9b9e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "print(2 ** 3)  # Exponentiation：指數運算\n",
    "print(7 // 6)  # Division：整除運算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 695,
     "status": "ok",
     "timestamp": 1606716605675,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "mbQFejWBxTwr",
    "outputId": "ec54882b-864d-48c8-e6f9-9dfca609f4ae"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(10 % 3)  # Modulo：求餘數"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "r6LpW04YWmJA"
   },
   "source": [
    "也可以搭配括號 `()` 執行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1314,
     "status": "ok",
     "timestamp": 1606716985868,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "qrjclaxLxY3m",
    "outputId": "ec4cc7b9-0a89-4b2b-f356-c69cccbc9d9c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# 小括號 () 的應用\n",
    "print(1 + 2 * 3)    # 沒有小括號\n",
    "print((1 + 2) * 3)  # 使用小括號"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "waG9RNiGWxV2"
   },
   "source": [
    "如果需要表示數值之間的相對關係，可以使用比較運算子，而比較的結果將會以布林值 (Boolean) 回傳\n",
    "\n",
    "> 備註：不認識布林值？不用擔心，我們很快地會在下一個小節提到布林值！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 810,
     "status": "ok",
     "timestamp": 1606717032666,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "KphgJknWyz6O",
    "outputId": "00f8cdee-90be-40d5-bca4-987fdb387570"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(5 > 3)    # 大於\n",
    "print(1 >= 10)  # 大於或等於"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1009,
     "status": "ok",
     "timestamp": 1606717061023,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "SICT2VB8zAj6",
    "outputId": "95a04877-9447-4246-e0c7-e89f60e75c04"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(7 < 4)   # 小於\n",
    "print(2 <= 9)  # 小於或等於"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 509,
     "status": "ok",
     "timestamp": 1606717163191,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "DgawRXrlzLNV",
    "outputId": "c9f26117-3dad-4347-ba01-c2c14bc13946"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(3 == 1 + 2)  # 等於\n",
    "print(8 != 9)      # 不等於"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "PHlv9G4SzvLF"
   },
   "source": [
    "### 浮點數計算的陷阱"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "xaH4Zy97YKOJ"
   },
   "source": [
    "由於大部分的電腦的浮點數計算都是透過二進位小數來表示，而目前幾乎所有浮點數的計算都採用了 IEEE 754 的標準來執行，所以可能會顯示近似值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 817,
     "status": "ok",
     "timestamp": 1607398832383,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "HtXaS3t37bWj",
    "outputId": "dee97ed8-118b-427e-fed9-243cf68505d5"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.30000000000000004\n"
     ]
    }
   ],
   "source": [
    "print(0.1 + 0.1 + 0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2Ibv2h97b9dk"
   },
   "source": [
    "Python 官方文件中也提供了解決方法，大致羅列：\n",
    "\n",
    "* 採用 [Decimal module](https://docs.python.org/3/library/decimal.html#module-decimal)\n",
    "* 採用 [Fractions module](https://docs.python.org/3/library/fractions.html#module-fractions)\n",
    "* （推薦）採用 [Numpy](https://numpy.org/) / [Scipy](https://scipy.org/)\n",
    "\n",
    "References:\n",
    "\n",
    "* [Floating Point Arithmetic: Issues and Limitations - Python documentation](https://docs.python.org/3/tutorial/floatingpoint.html)\n",
    "   * [浮點數運算：問題與限制](https://docs.python.org/zh-tw/3/tutorial/floatingpoint.html)\n",
    "* [IEEE 754 - Wikipedia](https://en.wikipedia.org/wiki/IEEE_754)\n",
    "   * [IEEE 754 - 維基百科](https://zh.wikipedia.org/zh-tw/IEEE_754)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6cxTFYYjABDa"
   },
   "source": [
    "## Booleans：布林值，標示是、否的資料型態"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ZzYeX7s-BDLy"
   },
   "source": [
    "布林值用 `True` 表示真值、而用 `False` 表示假值。\n",
    "\n",
    "布林值也是整數 (`int`) 型態的子集合，用 `0` 表示 `False`，而其他的數值均表示 `True`。\n",
    "\n",
    "> 備註：初學的時候最容易忘記的是布林值的 `True` 跟 `False` 的首字都是大寫！\n",
    "\n",
    "References:\n",
    "\n",
    "* [bool - Python documentation](https://docs.python.org/3/library/functions.html#bool)\n",
    "* [Truth Table - Wikipedia](https://en.wikipedia.org/wiki/Truth_table)\n",
    "   * [真值表 - 維基百科](https://zh.wikipedia.org/zh-tw/%E7%9C%9F%E5%80%BC%E8%A1%A8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 790,
     "status": "ok",
     "timestamp": 1607399787486,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "LjwG2y7efhG7",
    "outputId": "255ba635-3b10-4ba3-a4d0-c3e1d618d7d6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 736,
     "status": "ok",
     "timestamp": 1607399686002,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "rdxDij7wAwvy",
    "outputId": "737d3d54-e52a-48ce-ef41-30b86b022aad"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(True)   # 是\n",
    "print(False)  # 否"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "print(type(True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1056,
     "status": "ok",
     "timestamp": 1607399969411,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "2VPLB5oPAy6f",
    "outputId": "61c67bac-5d17-47b9-e293-8609f9830c52"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(True == 1)   # 驗證 0 以外的數字是否代表 True\n",
    "print(False == 0)  # 驗證 0 以外的數字是否代表 False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FMWOLKalgUz0"
   },
   "source": [
    "在 Python 中，有兩個類似的運算子，是可以用來比較兩物件是否相等的：\n",
    "\n",
    "* `==`：相等 (Equality) 運算子\n",
    "* `is`：標示 (Identity) 運算子\n",
    "\n",
    "若要用一個表達式與布林值相比較，正確的用法應該是用標示運算子 `is` 來比較（即使兩者回傳的結果相當，仍會有些許的效率差異）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1213,
     "status": "ok",
     "timestamp": 1606722963853,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "BHb_Yk6mE26n",
    "outputId": "fc0e3c45-d26b-4456-a6db-fd21f5a82422"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 用 `is` 來驗證兩表達式是否相等\n",
    "print((3 + 1 == 4) is True)\n",
    "print((3 + 1 == 4) == True)  # 此方式執行較慢"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "stHJ2InVzk9n"
   },
   "source": [
    "而標示運算子除了 `is`（是）以外，常用的還有 `or`（或）與 `not`（非）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1718,
     "status": "ok",
     "timestamp": 1606877864961,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "JEY-yiInYGcK",
    "outputId": "2a2e6422-3522-45c4-97ef-1ee6f0f5c53a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# not 的用法：將真偽逆轉\n",
    "print(not(True) is False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1162,
     "status": "ok",
     "timestamp": 1606877881312,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "-JQeT5EbYkJU",
    "outputId": "0b7f208d-e1b6-4529-8b64-afbf2a026d46"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# or 的用法：任意表達式為真，即為真\n",
    "print(True or False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "qwpCcOnoYp7S"
   },
   "source": [
    "## Strings：字串的資料型態"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TpH4Sd4Pdkhf"
   },
   "source": [
    "若需要表達一串字元，可以將字元至於成對單引號（`'`, Single quote）或是雙引號（`\"`, Double quote）之中。型別名稱為 `str`。\n",
    "\n",
    "而若有特殊符號需作為字串表示時，可以使用倒斜線（`\\`, Backslash） 作為「逃脫字元 (Escape character)」，讓逃脫字元之後的字元被視為一般字元來顯示。\n",
    "\n",
    "References:\n",
    "\n",
    "* [Text Sequence Type — str - Python Documentation](https://docs.python.org/3/library/stdtypes.html#textseq)\n",
    "* [PEP 257 -- Docstring Conventions](https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1723,
     "status": "ok",
     "timestamp": 1606878735926,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "OjzNKl9NbwIc",
    "outputId": "3d34eb53-fe65-435e-f937-bdace9c0c027"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello world!\")        # 試著建立一個字串物件\n",
    "print(type(\"Hello world!\"))  # 驗證物件是否為字串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1410,
     "status": "ok",
     "timestamp": 1606878830489,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "sGLbiGb6b6cJ",
    "outputId": "124684e2-8025-422c-a813-be201c8a2a49"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We can also use single quotes\n",
      "Also, double quotes are fine\n"
     ]
    }
   ],
   "source": [
    "print('We can also use single quotes')  # 使用成對單引號 '' 來建立字串\n",
    "print(\"Also, double quotes are fine\")   # 使用成對雙引號 \"\" 來建立字串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 967,
     "status": "ok",
     "timestamp": 1607406104075,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "rX7QN-yd2ugO",
    "outputId": "55db34e5-6ea8-402d-b5b5-1b0bbf6114d2"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey, \"Vivi\" is my name. Don't forget!\n"
     ]
    }
   ],
   "source": [
    "# 逃脫字元\n",
    "# 範例：在使用雙引號的字串裡顯示雙引號\n",
    "print(\"Hey, \\\"Vivi\\\" is my name. Don't forget!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "_GADkSNz3s4b"
   },
   "source": [
    "若是要建立多行的字串，可以使用三個重複的單引號或雙引號來建立字串物件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is an one-line string\n",
      "And these are\n",
      "multiline strings\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"\"\"This is an one-line string\"\"\")\n",
    "print(\"\"\"And these are\n",
    "multiline strings\n",
    "\"\"\")  # ''' also works!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "多行字串常見的用途，是作為函式或是方法的文件字串 (Docstrings)："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 搭配內建函式 help()，我們將可以查看物件的說明文件內容！\n",
    "# 例：查看 print() 的用法\n",
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function foo in module __main__:\n",
      "\n",
      "foo()\n",
      "    This is the docstrings\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 接著我們來驗證一下 Docstrings 的製作\n",
    "def foo():\n",
    "    \"\"\"\n",
    "    This is the docstrings\n",
    "    \"\"\"\n",
    "    pass\n",
    "\n",
    "help(foo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "> 看不懂 `def` 在做什麼嗎？其實就是建立一個函式。不知道為什麼這樣用？別用擔心，後面的章節會講解到！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "### 索引 (Indexing) 與分割 (slicing) 字串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "在 Python 中，並沒有單一字元的 `char` 型態，而 `str` 字串型態可以視為多個單一字元的集合，所以可以從中擷取部分的內容。\n",
    "\n",
    "用來指定擷取的內容位置是索引值 (Index)。在電腦程式的世界裡，通常第一個物件的索引值都是 `0`，之後的索引值則為 `1`, `2`, `3`......。Python 還有提供由後往前數的功能，而最後一個字元往前算的索引值則為 `-1`, `-2`, `-3`......。\n",
    "\n",
    "指定索引值的格式為：`[start:stop:step]`，其中：\n",
    "\n",
    "* `start`：欲擷取字串中首字的索引值\n",
    "* `stop`：欲擷取字串中末字的索引值 + 1（也就是擷取的內容不包含 `stop` 位置的字元），此值可忽略\n",
    "* `step`：從擷取首字的位置起算，每隔幾個字元擷取一次，預設為 `1`，此值可忽略\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "# 試著指定 start 索引值來取得字串中的單一字元\n",
    "print(\"String\"[3])\n",
    "print(\"String\"[-3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ring\n"
     ]
    }
   ],
   "source": [
    "# 試著指定 start 以及 stop 的索引值，來擷取數個字元\n",
    "print(\"String\"[2:6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Str\n",
      "ring\n"
     ]
    }
   ],
   "source": [
    "print(\"String\"[:3])  # 若不指定 start，則預設從第一個字元開始擷取\n",
    "print(\"String\"[2:])  # 若不指定 stop，則預設擷取到最後一個字元"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tig\n",
      "tig\n"
     ]
    }
   ],
   "source": [
    "# 試著指定 step\n",
    "print(\"String\"[1:6:2])\n",
    "print(\"String\"[1::2])  # 可以不指定 start 或 stop 的任意一項"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "### 字串相關小技巧"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "在顯示字串時，可以將字串用不同的方式組合在一起，提高閱讀性："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n",
      "Hello world!\n",
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello \" + \"world!\")  # 多個字串可以用 + 號連接成一個字串\n",
    "print(\"Hello \"\"world!\")     # 可以將字串連續放置在 print() 函式內\n",
    "print(\"Hello\", \"world!\")    # 字串間如果用逗號 \",\" 隔開，則在顯示時會自動在兩倆之間加上空格"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "而使用 `in`: 包含測試運算子，可以驗證某字串是否包含在某個字串之內"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print('lo' in \"Hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "### 字串方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "source": [
    "字串帶有一些內建的方法 (methods) 可以使用，例如大小寫轉換、尋找字元、取代、去除字串內的空白字元⋯⋯等等。\n",
    "\n",
    "> 「方法 (methods)」容易與「函式 (functions)」稍微產生混淆。下一個章節我們會把這兩個要素放在一起講解，現在讓我們先學會操作一些實用的方法吧！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO WORLD!\n",
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello world!\".upper())  # 將字串轉換為大寫\n",
    "print(\"Hello world!\".lower())  # 將字串轉換為小寫"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(\"string\".find('ring'))  # 在字串中，搜尋特定字串第一次出現的位置的索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "strike\n"
     ]
    }
   ],
   "source": [
    "print(\"string\".replace(\"ng\", \"ke\"))  # 將字串取代為另一個字串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "string<EOL>\n",
      "string    <EOL>\n",
      "    string<EOL>\n"
     ]
    }
   ],
   "source": [
    "print(\"    string    \".strip() + \"<EOL>\")   # 消除字串左側與右側的所有空格\n",
    "print(\"    string    \".lstrip() + \"<EOL>\")  # 消除字串左側的所有空格\n",
    "print(\"    string    \".rstrip() + \"<EOL>\")  # 消除字串右側的所有空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', '1', '2', '3', '5', '8', '13', '21']\n"
     ]
    }
   ],
   "source": [
    "print(\"1,1,2,3,5,8,13,21\".split(\",\"))  # 將字串以特定字元為界線，將其分割成許多分開的字串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 1311,
     "status": "ok",
     "timestamp": 1606888701236,
     "user": {
      "displayName": "黃種平",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjCoYcYDAMAK-J-MvClxwWJY0fvKYsny6BL962ZNA=s64",
      "userId": "12486487678081777487"
     },
     "user_tz": -480
    },
    "id": "m6-SDtwLcRlU",
    "outputId": "ecf54006-4c54-4950-c482-7fb01cef45ba"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E-N-G-L-I-S-H\n"
     ]
    }
   ],
   "source": [
    "print(\"-\".join(\"ENGLISH\"))  # 將字串中的每個字元，用指定的字元相互連接起來"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyNVFSxEMJpAkMijMh0Asom/",
   "collapsed_sections": [],
   "name": "Python Basics - Chapter 2 - Datatypes and Operators.ipynb",
   "provenance": [],
   "toc_visible": true
  },
  "jupytext": {
   "encoding": "# -*- coding: utf-8 -*-",
   "formats": "ipynb,py:light"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
